﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace WFL.Data.SqlClient
{
    public class SqlOperationWithContext : SqlOperation
    {
        private static Context _defaultContext = new SqlContext();

        public Context Context
        {
            get;
            set;
        }

        public SqlOperationWithContext()
            : this(null)
        {
            
        }

        public SqlOperationWithContext(string connectionString)
            : base(connectionString)
        {
            Context = _defaultContext;
        }

        protected override SqlConnection GetContextConnection()
        {
            SqlConnectionProxy connProxy = Context.GetConn() as SqlConnectionProxy;
            return connProxy != null ? connProxy.Connection : null;
        }

        protected override SqlTransaction GetContextTransaction()
        {
            SqlConnectionProxy connProxy = Context.GetConn() as SqlConnectionProxy;
            if (connProxy != null
                && connProxy.TransactionProxy != null)
            {
                return connProxy.TransactionProxy.Transaction;
            }
            return null;
        }
    }
}
